using System;
using System.Collections.Generic;
using System.Text;
using Mud.Common.Messages;
using Mud.Common;

namespace Mud.LoadTest
{
	public static class TestMessages
	{

		public static void TestMessage()
		{
			Logger.Log(LogType.Info, "Test", "Starting to test.");
			TestReflectionSerialization();
			TestReflectionSerialization();
			TestReflectionSerialization();
			TestReflectionSerialization();

			TestHandSerialization();
			TestHandSerialization();
			TestHandSerialization();
			TestHandSerialization();

			TestReflectionSerialization();
			TestHandSerialization();
		}

		private static void TestReflectionSerialization()
		{
			IMessage msg = CreateReflectionMessage();
			TimeFormater.StartingTimeIndex = Environment.TickCount;
			Logger.Log(LogType.Info, "Test", "Testing serialization with reflection on 20000 messages.");
			for (int i = 0; i < 20000; i++)
			{
                int unused;
                byte[] serialized = MessageUtil.Pack(msg).Unpack();
                msg.Deserialize(BytePackage.Pack(serialized, 0, serialized.Length, out unused));
			}
			Logger.Log(LogType.Info, "Test", "Finished. Time elapsed {0}.", TimeFormater.FormatTimeIndex(Environment.TickCount));
		}

		private static void TestHandSerialization()
		{
			IMessage msg = CreateHandMessage();
			TimeFormater.StartingTimeIndex = Environment.TickCount;
            Logger.Log(LogType.Info, "Test", "Testing hand serialization on 2000 messages.");
            for (int i = 0; i < 2000; i++)
			{
                int unused;
                byte[] serialized = MessageUtil.Pack(msg).Unpack();
                msg.Deserialize(BytePackage.Pack(serialized, 0, serialized.Length, out unused));
            }
			Logger.Log(LogType.Info, "Test", "Finished. Time elapsed {0}.", TimeFormater.FormatTimeIndex(Environment.TickCount));
		}

		private static IMessage CreateReflectionMessage()
		{
			Mud.Common.Messages.MessageEventCreatureAppeared msg = new Mud.Common.Messages.MessageEventCreatureAppeared();
			msg.Creature = new Mud.Common.Messages.MessageStructureCreatureInfo();
			msg.Creature.ObjectInfo.AreaName = "Test";
			msg.Creature.ObjectInfo.LocationX = 5;
			msg.Creature.ObjectInfo.LocationY = 10;
			msg.Creature.ObjectInfo.ObjectId = 15;
			msg.Creature.ObjectInfo.ObjectName = "Name";
			msg.Creature.ObjectInfo.Orientation = Mud.Common.Physical.Orientation.North;
			return msg;
		}


		private static MessageStructureAreaLandInfo CreateHandMessage()
		{
			int dimension = 100;
			MessageStructureAreaLandInfo msg = new MessageStructureAreaLandInfo();
			msg.Height = dimension;
			msg.Width = dimension;
			msg.Terrain = new Mud.Common.Physical.TerrainType[dimension, dimension];
			for (int i = 0; i < msg.Width; i++)
				for (int j = 0; j < msg.Height; j++)
					msg.Terrain[i, j] = Mud.Common.Physical.TerrainType.Dirt;
			return msg;
		}

	}
}
